<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入商品名称" clearable @clear="handleQuery"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品所需积分" prop="beginGoodsScore" label-width="130px">
        <el-input v-model="queryParams.beginGoodsScore" placeholder="请输入商品所需积分" clearable @clear="handleQuery" />

      </el-form-item>
      <el-form-item label="-" prop="endGoodsScore" label-width="10px">
        <el-input v-model="queryParams.endGoodsScore" placeholder="请输入商品所需积分" clearable @clear="handleQuery" />
      </el-form-item>
      <el-form-item label="商品分类" prop="goodsTypeId" label-width="80px">
        <el-select v-model="queryParams.goodsTypeId" placeholder="请选择" clearable @clear="handleQuery">
          <el-option v-for="item in goodsTypeList" :key="item.id" :label="item.typeName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['score:goods:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['score:goods:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['score:goods:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['score:goods:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品所需积分" align="center" prop="goodsScore" />
      <el-table-column label="商品图片" align="center" prop="goodsPicture">
        <template slot-scope="scope">
          <el-image :src="scope.row.goodsPicture">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="商品介绍" align="center" prop="goodsIntroduce">
        <template slot-scope="scope">
          <el-button type="text" @click="openjs(scope.row)">点击查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="商品分类" align="center" prop="goodsType.typeName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['score:goods:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['score:goods:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 商品介绍查看 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="80%" :before-close="handleClose">
      <div class="quill_div"><quill-editor v-model="context"></quill-editor></div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加或修改积分商品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品所需积分" prop="goodsScore">
          <el-input v-model="form.goodsScore" placeholder="请输入商品所需积分" />
        </el-form-item>
        <el-form-item label="商品图片" prop="goodsPicture">
          <el-upload action="http://localhost/dev-api/fileUpload" list-type="picture-card" :on-success="handleSuccess"
            :on-remove="handleRemove" :file-list="fileList" multiple :limit="4" :headers="headers">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品分类" prop="goodsTypeId">
          <el-select v-model="form.goodsTypeId" placeholder="请选择商品分类">
            <el-option v-for="item in goodsTypeList" :key="item.id" :label="item.typeName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品介绍" prop="goodsIntroduce" height="500px">
          <!-- <div class="quill_div"><quill-editor v-model="form.goodsIntroduce"></quill-editor></div> -->
          <div class="quill_div">
            <editor v-model="form.goodsIntroduce" :min-height="192" />
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listGoods,
    getGoods,
    delGoods,
    addGoods,
    updateGoods
  } from "@/api/score/goods";
  import {
    listGoodsType,
    getGoodsType,
    delGoodsType,
    addGoodsType,
    updateGoodsType
  } from "@/api/score/goodsType";
  import {
    getToken
  } from '@/utils/auth'
  export default {
    name: "Goods",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 积分商品管理表格数据
        goodsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          goodsName: null,
          goodsScore: null,
          goodsTypeId: null,
          beginGoodsScore: null,
          endGoodsScore: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        //分类数据
        goodsTypeList: [],
        //图片上传
        dialogImageUrl: '',
        dialogImg: false,
        //图片列表
        fileList: [],

        headers: {
          'Authorization': 'Bearer ' + getToken()
        },
        //商品介绍
        dialogVisible: false,
        //内容
        context: null,
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 商品介绍预览*/
      openjs(row) {
        this.context = row.goodsIntroduce;
        this.dialogVisible = true;
      },
      /** 商品介绍关闭*/
      handleClose(done) {
        console.log(done);
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      /** 图片移除*/
      handleRemove(file, fileList) {
        this.fileList = fileList.map(item => ({
          name: item.url,
          url: item.url
        }));
        this.form.goodsPicture = JSON.stringify(this.fileList)
      },
      /** 查询积分商品分类列表 */
      handleSuccess(res, file) {
        this.fileList.push({
          name: res.data,
          url: res.data
        });
        this.form.goodsPicture = JSON.stringify(this.fileList)
      },
      /** 查询积分商品分类列表 */
      getSortList() {
        listGoodsType().then(response => {
          this.goodsTypeList = response.rows;
        });
      },
      /** 查询积分商品管理列表 */
      getList() {
        this.loading = true;
        this.getSortList();
        listGoods(this.queryParams).then(response => {
          this.goodsList = response.rows.map(item => {
            item.goodsPicture = JSON.parse(item.goodsPicture)[0].url;
            console.log(item.goodsPicture);
            return item;
          });

          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          goodsName: null,
          goodsScore: null,
          goodsPicture: null,
          goodsIntroduce: null,
          goodsTypeId: null,
          del: null
        };
        this.fileList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加积分商品管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getGoods(id).then(response => {
          this.form = response.data;
          this.fileList = JSON.parse(this.form.goodsPicture);
          this.open = true;
          this.title = "修改积分商品管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateGoods(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addGoods(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除积分商品管理编号为"' + ids + '"的数据项？').then(function() {
          return delGoods(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('score/goods/export', {
          ...this.queryParams
        }, `goods_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>

<style scoped>
  .quill-editor {
    height: 400px;
    margin-bottom: 40px;
  }
</style>
